package 变长滑动窗口1

func maxSubarrayLength(nums []int, k int) (ans int) {
	left := 0
	numCount := map[int]int{}
	for i, num := range nums {
		numCount[num]++
		for numCount[num] > k {
			// 获取左指针指向的元素（即将移出窗口的元素）
			leftNum := nums[left]
			numCount[leftNum]--
			if numCount[leftNum] == 0 {
				delete(numCount, leftNum)
			}
			left++
		}
		ans = max(ans, i-left+1)
	}
	return
}
